//
//  CZNetWorkTools.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/26.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
import AFNetworking
//请求方式 枚举
enum MethodType: String {
    case GET = "GET"
    case POST = "POST"
}

class CZNetWorkTools: AFHTTPSessionManager {
    //单例
    static let sharedTools: CZNetWorkTools = {
        let tools = CZNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
        
    }()
    
    //网络请求数据封装
    func request(method:MethodType, urlString: String, parameters: AnyObject?, success: (response: AnyObject?)->(), failure: (error: NSError)->()){
        // GET 请求
        if method == .GET {
            self.GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
                    
            })
        }else {
            // POST 请求
            self.POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
                    
            })
        }
    }
    
}

extension CZNetWorkTools {
    //请求token
    func oauthLoadUserAccount(code: String,success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parame = [
            "client_id":APPKEY,
            "client_secret":APPSECRET,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":REDIRECT_URI
        ]
        self.request(MethodType.POST, urlString: urlString, parameters: parame, success: success, failure: failure)
        
    }
    //请求个人数据
    func oauthLoadUserInfo(userAccount: CZUserAcount, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parame = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        self.request(MethodType.GET, urlString: urlString, parameters: parame, success: success, failure: failure)
        
    }
    //请求微博数据
    func oauthWeiboInfo(since_id: Int64,max_id: Int64, success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parame = [
            "access_token":CZUserAccountTools.sharedUserAccountTools.access_token!,
            "since_id":"\(since_id)",
            "max_id":"\(max_id)"
        ]
        
        request(.GET, urlString: urlString, parameters: parame, success: success, failure: failure)
        
    }
    //请求发送文字微博数据
    func updateWeibo(status: String,success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        let  parame = [
            "access_token":CZUserAccountTools.sharedUserAccountTools.access_token!,
            "status":status
        ]
        request(MethodType.POST, urlString: urlString, parameters: parame, success: success, failure: failure)
        
    }
    //请求带图片微博数据
    func uploadWeibo(status: String,imageList: [UIImage],success: (response: AnyObject?)->(), failure: (error: NSError)->()) {
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        let parame = [
            "access_token":CZUserAccountTools.sharedUserAccountTools.access_token!,
            "status":status,
        ]
        POST(urlString, parameters: parame, constructingBodyWithBlock: { (formdata) in
            for image in imageList {
                let imageData = UIImagePNGRepresentation(image)!
                formdata.appendPartWithFileData(imageData, name: "pic", fileName: "oooo", mimeType: "application/octet-stream")
            }
            }, progress: nil, success: { (_, response) in
                success(response: response)
            }) { (_, error) in
                
                failure(error: error)
        }
    }
    
}